package com.joinlabs.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.joinlabs.entity.User;
import com.joinlabs.service.IUserService;
import com.joinlabs.util.MD5Utils;
import com.joinlabs.util.SendCodeUtil;


@Controller
@RequestMapping("/user")
@SessionAttributes(names="user")
public class RegisterAction {
	@Resource
	private IUserService userService;
	@RequestMapping("/loginreg/register")
	//显示注册界面
	public ModelAndView showregister(){	
		return new ModelAndView("/loginreg/register");
	}
	
	//全部的注册逻辑	
	@RequestMapping("/register/*")
	public ModelAndView register(User user,ModelMap map,HttpServletRequest request, HttpServletResponse response) throws IOException{
		String ss = request.getServletPath();
		String userName = null;
		String userPassword = null;
		String userSurname = null;
		String userId = null;
		String bankId = null;
		String userPhone = null;
		
		//判断用户名是否重名
		if("/user/register/checkName".equals(ss)){	
			userName = user.getUserName();
			user = userService.select(userName);
			if(user != null){	
				response.setContentType("text/html;charset=utf-8");
				PrintWriter print  = response.getWriter();
				print.println("用户名已存在");
				print.close();
			}
		}else if("/user/register/reg".equals(ss)){
			userName = user.getUserName();
			userPassword = user.getUserPassword();
			userSurname = user.getUserSurname();
			userId = user.getUserId();
			bankId = user.getBankId();
			userPhone = user.getUserPhone();
			System.out.println(userPassword);
			map.addAttribute("user", user);
		}else if("/user/register/gainNote".equals(ss)){     //获取短信验证码的逻辑
			userPhone = user.getUserPhone();
			String code = request.getParameter("code");
			user.setUserPhone(userPhone);//手机号码
			String uphone = request.getParameter("uphone");
			String sendsm = SendCodeUtil.sendsms(uphone);
			System.out.println(sendsm);
			JSONObject jo = (JSONObject) JSONObject.parse(sendsm);
			String notecode = jo.getString("obj");
			System.out.println(notecode);
			request.getSession().setAttribute("notecode",notecode);
		}
		if(userName != null && userPassword !=null && userSurname != null && userId != null && bankId != null && userPhone != null){
			String pwd = MD5Utils.MD5Encode(userPassword, "utf-8");//加密注册密码
			userService.userregister(userId, userName, pwd, userSurname, userPhone, null, bankId, 1, null, 0, null, null, null);
			return new ModelAndView("/loginreg/registersuccess");
		}
		return null;
	}
	
	@RequestMapping("/checkphone")
	@ResponseBody
	public String checkphone(User user){
		//判断手机号是否已被使用
		String userPhone = user.getUserPhone();
		user = userService.checkphone(userPhone);
		if(user != null){
			return "false";
		}
		return "true";
	}
	
	
	//判断短信验证码是否正确
	@RequestMapping("/sendcode")
	@ResponseBody
	public String checkcode(HttpServletRequest request){
		String sendcode = request.getParameter("sendcode");
		String code = (String) request.getSession().getAttribute("notecode");
		if(code != null && sendcode.equals(code)){
			return "true";
		}
		return "false";
	}
}
